package com.gxsf.wechatbook.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxsf.wechatbook.pojo.TokenList;
import com.gxsf.wechatbook.pojo.User;
import com.gxsf.wechatbook.repo.TokenListRepository;
import com.gxsf.wechatbook.repo.UserRepository;
import com.gxsf.wechatbook.util.Error;
import com.gxsf.wechatbook.util.Md5Util;
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserRepository userRepository;
    @Autowired
    TokenListRepository tokenListRepository;

    public String login(String phone, String password) {
        if (phone != null && !phone.isEmpty() && password != null && !password.isEmpty()) {
            User user = this.userRepository.findByPhone(Long.parseLong(phone));
            if (user == null) {
                Error user_not_find = Error.error(500, "用户不存在！");
                return JSON.toJSONString(user_not_find);
            } else {
                boolean compared = Md5Util.compared(password, user.getPassword());
                if (compared) {
                    TokenList tokenList = new TokenList();
                    String phoneHex = DigestUtils.md5Hex(phone.getBytes(StandardCharsets.UTF_8));
                    tokenList.setTokenName(phoneHex);
                    tokenList.setMessage(JSON.toJSONString(user));
                    TokenList save = (TokenList)this.tokenListRepository.save(tokenList);
                    return JSON.toJSONString(save);
                } else {
                    Error user_not_find = Error.error(404, "账号密码错误！");
                    return JSON.toJSONString(user_not_find);
                }
            }
        } else {
            Error user_not_find = Error.error(404, "请输入用户名或密码");
            return JSON.toJSONString(user_not_find);//Failed to set token, parameter has empty
        }
    }

    public Object updatePassword(String token, String pass) {
        TokenList tokenList = this.tokenListRepository.findFirst1ByTokenNameAndStatusOrderByTokenIdDesc(token, 0L);
        if (tokenList == null) {
            return Error.error(404, "找不到token信息，会话已超时或未登录");
        } else {
            String message = tokenList.getMessage();
            JSONObject jsonObject = JSON.parseObject(message);
            User user = (User)JSON.toJavaObject(jsonObject, User.class);
            user.setPassword(DigestUtils.md5Hex(pass.getBytes(StandardCharsets.UTF_8)).toUpperCase());
            this.userRepository.save(user);
            tokenList.setUnTime(0L);
            return this.tokenListRepository.save(tokenList);
        }
    }

    public Object updateName(String token, String name) {
        TokenList tokenList = this.tokenListRepository.findFirst1ByTokenNameAndStatusOrderByTokenIdDesc(token, 0L);
        if (tokenList == null) {
            return Error.error(404, "找不到token信息，会话已超时或未登录");
        } else {
            String message = tokenList.getMessage();
            JSONObject jsonObject = JSON.parseObject(message);
            User user = (User)JSON.toJavaObject(jsonObject, User.class);
            user.setName(name);
            this.userRepository.save(user);
            tokenList.setUnTime(0L);
            return this.tokenListRepository.save(tokenList);
        }
    }
}
